package com.hexinfo.dmpro.component.common.util;

import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@Slf4j
public class DataSourceUtil {

    private static final String HIVE_SOURCE_BEAN_NAME = "connPoolHiveSource";
    private static final String HIVE_TARGET_BEAN_NAME = "connPoolHiveTarget";
    private static final String IMPALA_SOURCE_BEAN_NAME = "connPoolImpalaSource";
    private static final String IMPALA_TARGET_BEAN_NAME = "connPoolImpalaTarget";

    public static Connection getHiveSourceConnection() {
        try {
            DataSource s1 = SpringContextUtil.getBean(HIVE_SOURCE_BEAN_NAME, DataSource.class);
            return s1.getConnection();
        } catch (Exception e) {
            throw new RuntimeException("[数据比对组件]- 获取Hive源Connection异常:" + e.getMessage(), e);
        }
    }

    public static Connection getHiveTargetConnection() {
        try {
            DataSource s1 = SpringContextUtil.getBean(HIVE_TARGET_BEAN_NAME, DataSource.class);
            return s1.getConnection();
        } catch (Exception e) {
            throw new RuntimeException("[数据比对组件]- 获取Hive目标Connection异常:" + e.getMessage(), e);
        }
    }

    public static Connection getImpalaSourceConnection() {
        try {
            DataSource s1 = SpringContextUtil.getBean(IMPALA_SOURCE_BEAN_NAME, DataSource.class);
            return s1.getConnection();
        } catch (Exception e) {
            throw new RuntimeException("获取Impala源Connection异常:" + e.getMessage(), e);
        }
    }

    public static Connection getImpalaTargetConnection() {
        try {
            DataSource s1 = SpringContextUtil.getBean(IMPALA_TARGET_BEAN_NAME, DataSource.class);
            return s1.getConnection();
        } catch (Exception e) {
            throw new RuntimeException("[数据比对组件]- 获取Impala目标Connection异常:" + e.getMessage(), e);
        }
    }

    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            log.error("[数据比对组件]- 关闭数据源Connection异常：{}", e.getMessage(), e);
        }
    }

    public static void close(Statement statement) {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (Exception e) {
            log.error("[数据比对组件]- 关闭数据源Statement异常：{}", e.getMessage(), e);
        }
    }

    public static void close(ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (Exception e) {
            log.error("[数据比对组件]- 关闭数据源ResultSet异常：{}", e.getMessage(), e);
        }
    }

    public static void close(Connection connection, Statement statement) {
        close(connection);
        close(statement);
    }

    public static void close(Statement statement, ResultSet resultSet) {
        close(statement);
        close(resultSet);
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        close(connection);
        close(statement);
        close(resultSet);
    }

    public static void close(Connection connection, ResultSet resultSet) {
        close(connection);
        close(resultSet);
    }
}
